const commoneUrl = "https://sanyue.scdzg.com.cn/dy.php?s="; //公共路径 
// const commoneUrl  = "https://zx.hufazaixian.cn "; //公共路径 
const token = uni.getStorageSync("token") //用户token
const type = uni.getStorageSync("type") //用户token


//post请求封装

function postRequest(url, data, type) {
	var promise = new Promise((resolve, reject) => {
		// 获取本地存储的token和sessionid
		var token = uni.getStorageSync('token');
		// var session_id = uni.getStorageSync('session_id');
		var session_id = encodeURIComponent(uni.getStorageSync('session_id'));
		
		
		// var postData = data;  固定值 
		var postData = Object.assign({},data,{aid:1, platform:'dy',session_id:session_id});
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: 'POST',
			// dataType:"jsonp",
			// jsonp:"callbackparam",//传递给请求处理程序或页面的，用以获得jsonp回调函数名的参数名
			// jsonpCallback:"success_jsonpCallback",
			header: {
				// 'token':'9abdd8e8-0e53-4240-9042-f289bd2c18aa',
				'token': uni.getStorageSync('token'),
				'platform':uni.getStorageSync("type")
			}, //token可以不要，看后端
			success: function(res) {
				if (res.data.code == 401) {
					// uni.navigateTo({
					// 	url: '/pages/login/login'
					// })
					console.log('token',token);
					uni.showModal({
						title: '提示',
						content: res.data.message,
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/tabs/my'
								})
							} else if (res.cancel) {
								uni.switchTab({
                                    url:'/pages/tab/index'
                                })
							}
						}
					});
				} else {
					resolve(res.data);
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		})
	});
	return promise;
}
//get请求封装
function getRequest(url, data) {
	var promise = new Promise((resolve, reject) => {
		// var postData = data;
		// 获取本地存储的token和sessionid
		var token = uni.getStorageSync('token');
		var session_id = encodeURIComponent(uni.getStorageSync('session_id'));
		// var session_id = encodeURIComponent('Ewffms4e9GKfhN%252FqtwZS94dWorv3ZQPhLvLHWQMSEcEX2PXg0IyqiWbx5gxvaYTc2mBBE6ief%252B4qmpBfJZoBv3qP412Ji4uAIp5tKvqFb%252B62%252FVCvH%252BkLXPsACGETD2ACrGLy9bvP5Ov%252B%252Fzyfspjizw%253D%253D');
		
		
		// var postData = data;  固定值 
		var postData = Object.assign({},data,{aid:1, platform:'dy',session_id:session_id});
		uni.request({
			url: commoneUrl + url,
			data: postData,
			method: "GET",
			dataType: 'json',
			header: {
				// 'token':'9abdd8e8-0e53-4240-9042-f289bd2c18aa',
				'token': uni.getStorageSync('token'),
				'platform':uni.getStorageSync("type")
			},
			success: function(res) {
				if (res.data.code == 401) {
					// uni.navigateTo({
					// 	url: '/pages/user/login'
					// })
					uni.showModal({
						title: '提示',
						content: res.data.msg,
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/user/login'
								})
							} else if (res.cancel) {
                                // uni.navigateBack({delta:1})
								uni.switchTab({
								    url:'/pages/tab/index'
								})
							}
						}
					});
				} else {
					resolve(res.data);
				}
			},
			error: function(e) {
				reject('网络出错');
			}
		});
	});
	return promise;
}

module.exports = {
	post: postRequest,
	get: getRequest,
	imgUrl: commoneUrl,
	token: token
};
